#ifndef PLANTASK_H
#define PLANTASK_H

#include <QString>
#include <QDateTime>
#include <QJsonObject>
#include "module_global.h"

/**
 * @brief 任务状态枚举
 * [FDD-2025-11] 扩展状态支持完整的任务生命周期
 */
enum class TaskStatus {
    Todo = 0,       // 待办
    Doing = 1,      // 进行中
    Done = 2,       // 已完成
    Blocked = 3,    // 被阻塞
    Archived = 4,   // 已归档
    Completed = 2   // 兼容旧代码：Completed = Done
};

/**
 * @brief [FDD-2025-11] 任务产出/证据数据结构
 */
struct MODULE_EXPORT TaskArtifacts {
    QString summary;              // 产出概述（文字描述）
    QStringList evidenceLinks;    // 证据/链接列表

    TaskArtifacts() = default;
    TaskArtifacts(const QString& sum, const QStringList& links = QStringList())
        : summary(sum), evidenceLinks(links) {}

    // JSON 序列化
    QJsonObject toJson() const;
    static TaskArtifacts fromJson(const QJsonObject& json);

    bool isEmpty() const { return summary.isEmpty() && evidenceLinks.isEmpty(); }
};

/**
 * @brief 日计划任务数据模型
 */
class MODULE_EXPORT PlanTask
{
public:
    PlanTask();
    PlanTask(const QString& id, const QString& title);
    
    // ========== 序列化 ==========
    QJsonObject toJson() const;
    static PlanTask fromJson(const QJsonObject& json);
    
    // ========== Getters ==========
    QString getId() const { return m_id; }
    QString getTitle() const { return m_title; }
    QString getDescription() const { return m_description; }
    QString getCompletionNote() const { return m_completionNote; }
    QDateTime getStartTime() const { return m_startTime; }
    QDateTime getEndTime() const { return m_endTime; }
    QDateTime getCompletedTime() const { return m_completedTime; }
    TaskStatus getStatus() const { return m_status; }
    bool hasReminder() const { return m_hasReminder; }
    QDateTime getReminderTime() const { return m_reminderTime; }
    int getSortOrder() const { return m_sortOrder; }
    QDateTime getCreatedTime() const { return m_createdTime; }
    QDateTime getUpdatedTime() const { return m_updatedTime; }

    // [FDD-2025-11] 新增字段的 Getters
    QString getDefinitionOfDone() const { return m_definitionOfDone; }
    QString getScopeDescription() const { return m_scopeDescription; }
    QString getBlockers() const { return m_blockers; }
    TaskArtifacts getArtifacts() const { return m_artifacts; }

    // ========== Setters ==========
    void setId(const QString& id) { m_id = id; m_updatedTime = QDateTime::currentDateTime(); }
    void setTitle(const QString& title) { m_title = title; m_updatedTime = QDateTime::currentDateTime(); }
    void setDescription(const QString& desc) { m_description = desc; m_updatedTime = QDateTime::currentDateTime(); }
    void setCompletionNote(const QString& note) { m_completionNote = note; m_updatedTime = QDateTime::currentDateTime(); }
    void setStartTime(const QDateTime& time) { m_startTime = time; m_updatedTime = QDateTime::currentDateTime(); }
    void setEndTime(const QDateTime& time) { m_endTime = time; m_updatedTime = QDateTime::currentDateTime(); }
    void setCompletedTime(const QDateTime& time) { m_completedTime = time; m_updatedTime = QDateTime::currentDateTime(); }
    void setStatus(TaskStatus status) {
        m_status = status;
        if (status == TaskStatus::Done || status == TaskStatus::Completed) {
            m_completedTime = QDateTime::currentDateTime();
        }
        m_updatedTime = QDateTime::currentDateTime();
    }
    void setReminder(bool enabled, const QDateTime& time = QDateTime()) {
        m_hasReminder = enabled;
        m_reminderTime = time;
        m_updatedTime = QDateTime::currentDateTime();
    }
    void setSortOrder(int order) { m_sortOrder = order; m_updatedTime = QDateTime::currentDateTime(); }

    // [FDD-2025-11] 新增字段的 Setters
    void setDefinitionOfDone(const QString& dod) { m_definitionOfDone = dod; m_updatedTime = QDateTime::currentDateTime(); }
    void setScopeDescription(const QString& scope) { m_scopeDescription = scope; m_updatedTime = QDateTime::currentDateTime(); }
    void setBlockers(const QString& blockers) { m_blockers = blockers; m_updatedTime = QDateTime::currentDateTime(); }
    void setArtifacts(const TaskArtifacts& artifacts) { m_artifacts = artifacts; m_updatedTime = QDateTime::currentDateTime(); }
    
    // ========== 辅助方法 ==========
    bool isValid() const;
    bool isOverdue() const;
    bool shouldRemindNow() const;
    int getDurationMinutes() const;
    
private:
    QString m_id;                   // 唯一标识符
    QString m_title;                // 任务标题
    QString m_description;          // 任务描述
    QString m_completionNote;       // 完成记录/总结
    QDateTime m_startTime;          // 开始时间
    QDateTime m_endTime;            // 结束时间
    QDateTime m_completedTime;      // 完成时间
    TaskStatus m_status;            // 任务状态
    bool m_hasReminder;             // 是否有提醒
    QDateTime m_reminderTime;       // 提醒时间
    int m_sortOrder;                // 排序顺序
    QDateTime m_createdTime;        // 创建时间
    QDateTime m_updatedTime;        // 更新时间

    // [FDD-2025-11] 新增字段
    QString m_definitionOfDone;     // 完成定义（DoD）
    QString m_scopeDescription;     // 范围/详细描述
    QString m_blockers;             // 阻塞点描述
    TaskArtifacts m_artifacts;      // 产出/证据
};

#endif // PLANTASK_H

